from django.shortcuts import render
from . import models

from django.http import JsonResponse, HttpResponse
from rest_framework.views import APIView
from rest_framework import exceptions
from rest_framework import serializers
from rest_framework.pagination import CursorPagination
import json


class configSer(serializers.ModelSerializer):
    class Meta:
        model = models.config
        fields = '__all__'


def getconfigOBJ(name):
    ser = models.config.objects.get(name=name)
    if ser.config:
        cof = json.loads(ser.config)
    config = configSer(instance=ser, many=False).data
    if ser.config:
        config['config'] = cof
    return config

class config(APIView):
    def get(self, request, *args, **kwargs):
        ret = {}
        name = request.GET['name']
        config = getconfigOBJ(name)
        return JsonResponse(config)
